package educar.controllers;

import educar.gui.IView;
import educar.gui.student.AddEnrollMateria;
import educar.idioma.Locale;
import educar.models.Cursa;
import educar.models.Materia;
import educar.models.Student;


/**
 * Permite al alumno vincularse a una materia
 */
public class AddEnrollMateriaController implements IController {
	private AddEnrollMateria view;
	private Student alumno;
	
	/**
	 * Constructor
	 * @param alumno que se desvincula a la materia
	 */
	public AddEnrollMateriaController(Student alumno) {
		this.alumno = alumno;
	}

	public void process(String model){
		if(null!=view) {
			/*obtengo elementos y compruebo validez*/
			Materia materia = view.getMateria();			
			if(materia==null ) {
				view.presentWarning(Locale.EMPTY_FIELDS, Locale.ERROR);
			}
			else {
				/*intento crear el nuevo cursa*/
				Cursa tmp = new Cursa(alumno, materia);
				boolean result = tmp.save();
				if (!result) {
					view.presentWarning(Locale.STUDENT_IS_IN, Locale.ERROR);
				}
				else {
					view.present(Locale.STUDENT+" "+Locale.ENROLLED);
					view.close();
				}
			}
		}
	}
	
	
	public void setView(IView view) {
		this.view = (AddEnrollMateria) view;		
	}	
}

